package com.zhizai.zhujian.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

public class IdGenerate {

	private static final IdGenerate idGenerate = new IdGenerate();

	private final static String DATE_FORMAT = "yyyyMMddHHmmssSSS";

	private static int times = 0;

	private String getTime() {

		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		Calendar c = Calendar.getInstance();
		String str = sdf.format(c.getTime());
		return str;
	}

	private int getTimes() {
		synchronized (IdGenerate.class) {
			if (times == 999) {
				times = 0;
			} else
				times++;
			return times;
		}
	}

	public String generateId(String seatCode) {
		int times = getTimes();
		if (seatCode == null || "".equals(seatCode))
			return getTime() + NumberUtils.format(String.valueOf(times), "000");
		else
			return seatCode + getTime()
					+ NumberUtils.format(String.valueOf(times), "000");
	}

	/**
	 * 
	 * @Title: 
	 * @Description: 产生唯一id： 时间+序号
	 * @return String 
	 * @throws
	 */
	public static String getId() {

		return idGenerate.generateId(null);
	}

	/**
	 * 
	 * @Title: 
	 * @Description: 产生唯一id： 席位+时间+序号
	 * @return String 
	 * @throws
	 */
	public static String getIdBySeatCode(String seatCode) {

		return idGenerate.generateId(seatCode);

	}

	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		String[] strs = uuid.toString().split("-");
		String temp = "";
		for(String str : strs) {
			temp += str;
		}
		return temp;
	}

	public static void main(String args[]) {
		
		for (int i = 0; i < 5; i++) {
			System.out.println(IdGenerate.getId());
			//System.out.println(IdGenerate.getIdBySeatCode("88888"));
//			System.out.println(IdGenerate.getUUID());
		}
	}

}
